var express = require('express');
const userController = require('../constroller/UserController');
var path = require("path")
var router = express.Router();
const multer = require('multer')
//const upload = multer({ dest: 'public/uploads/' })
const storage = multer.diskStorage({
    destination: (req, file, cb) => {
        cb(null, 'public/uploads/'); // 存储目录
    },
    filename: (req, file, cb) => {
        // 生成唯一文件名：时间戳+随机数+扩展名
        const uniqueSuffix = Date.now() + '-' + Math.round(Math.random() * 1e9);
        const ext = path.extname(file.originalname);
        cb(null, uniqueSuffix + ext);
    }
});

const upload = multer({ storage });



router.post("/login", userController.login)
router.post("/register", userController.reigister)
/**
 * 
 * @api {post} /api/user 添加账号
 * @apiName addUser
 * @apiGroup userGroup
 * @apiVersion  1.0.0
 * 
 * 
 * @apiParam  {String} username  账号
 * @apiParam  {String} password 密码
 * @apiParam  {String} email 邮箱
 * @apiParam  {String} image 头像路径
 * 
 * @apiSuccess (200) {json} name description
 * 
 * @apiParamExample  {type} Request-Example:
 * {
 *     username:"wen"
 *     password:"wen"
 *     email:"wen@qq.com"
 *     image:"uploads/123465-12313.png"
 * }
 * 
 * 
 * @apiSuccessExample {jons} Success-Response:
 * {
 *     status:1,
 *      message:"添加成功"
 * }
 * 
 * 
 */
router.post("/user", userController.add)
router.put("/user/:id", userController.update)
router.get("/user", userController.users)
router.delete("/user/:id", userController.delete)
router.get("/loginOut", userController.loginOut)
router.post("/upload", upload.single("file"), userController.upload)


module.exports = router;


